# -*- coding: utf-8 -*-

import tables as T
import numpy as N
from matplotlib.lines import Line2D

dummyDict = {'None':None}

class LPC(object):
    def __init__(self, dataDict=None):
        
        self.xlabel = None
        self.xy = None
        self.ydict = None
        self.xlabel = None
        self.ylabel = None
        self.data = None
        self.errorbars = False
        self.xerr = None#x error bar array
        self.yerr = None#y error bar arrays
        self.xerr_label = None
        self.yerr_label = None
        self.picker = 5.0
        self.ydict = None
        if dataDict is None:
            self.data = Line2D([0], [0])
            #print "No Dictionary was provided"
        else:
            yDataDict = dataDict[1]#extracts the y-numpy array(s)
            xDataDict = dataDict[0]
            #print yDataDict
            if len(yDataDict) is 1:#test whether there are multiple y-arrays
                if xDataDict.has_key('None'):
                    self.xy = False
                    #self.calcx = N.arange(len(self.ydata))
                    self.data = Line2D(N.arange(len(yDataDict.values()[0])),yDataDict.values()[0])
                    self.ylabel = yDataDict.keys()[0]
                    self.data.set_label(self.ylabel)
                    self.data.set_picker(self.picker)
                else:
                    self.xy = True
                    self.data = Line2D(xDataDict.values()[0],yDataDict.values()[0])
                    self.ylabel = yDataDict.keys()[0]
                    self.data.set_label(self.ylabel)
                    self.xlabel = xDataDict.keys()[0]
                    self.data.set_picker(self.picker)
                    
            else:
                if xDataDict.has_key('None'):
                    self.xy = False
                #print "This class can only take and XY pair!!"
                    ylist = []#creates an empty list to add additional LPCs
                    for item in yDataDict.iteritems():
                        #this loop continues to create a new dictionary of 
                        #LPC which will then be plotted individually
                        temp_LPC = LPC([dummyDict,dict([item])])
                        ylist.append((temp_LPC.ylabel, temp_LPC))
                        
                    self.ydict = dict(ylist)
                else:
                    self.xy = True
                    ylist = []#creates an empty list to add additional LPCs
                    for item in yDataDict.iteritems():
                        #this loop continues to create a new dictionary of 
                        #LPC which will then be plotted individually
                        temp_LPC = LPC([xDataDict,dict([item])])
                        ylist.append((temp_LPC.ylabel, temp_LPC))
                        
                    self.ydict = dict(ylist)

        
    def get_xlabel(self):
        return self.xlabel
    
    def get_ydict(self):
        return self.ydict
    
    def clear_xerr(self):
        self.xerr = None
        self.xerr_label = None
        if self.yerr is None:#want to make sure not to set to false when in fact it is still true
            self.errorbars = False
    
    def clear_yerr(self):
        self.yerr = None
        self.yerr_label = None
        if self.xerr is None:
            self.errorbars = False
    
    def set_xerr(self,  xerr_array,  name = None):
        if type(xerr_array) is N.ndarray:
#            print len(xerr_array),  len(self.data.get_xdata())
#            print type(self.data.get_xdata())
#            print self.data.get_xdata()[0]
#            print self.data.get_xdata()
            if len(xerr_array) == len(self.data.get_xdata()):
                self.xerr = xerr_array
                self.errorbars = True
                if name:#test for string?
                    self.xerr_label = name
        else:
            print "The length of the y error vector does not match"
    
    def set_yerr(self,  yerr_array,  name = None):
        if type(yerr_array) is N.ndarray:
            if len(yerr_array) == len(self.data.get_ydata()):
                self.yerr = yerr_array
                self.errorbars = True
                if name:#test for string?
                    self.yerr_label = name
        else:
            print "The length of the y error vector does not match"
            
    def get_xerr(self):
        return self.xerr
        
    def get_yerr(self):
        return self.yerr
    
    def set_data(self,  Line2D_Instance):
        self.data = Line2D_Instance
        
    
if __name__ == "__main__":
    x = N.arange(0,10, N.random.random())
    y = N.cos(x-N.random.random())
    y2 = N.sin(x-N.random.random())
    y3 = 2*N.sin(x-N.random.random())
    #data2plot = LinePlotClass([{'x':x},{'y':y}])
    xdict = {'x':x}
    #xdict = {'None':None}
    #ydict = {'y':y, 'y2':y2, 'y3':y3}
    ydict = {'y':y}
    #print type(ydict)
    #data2dict = LinePlotClass([xdict,ydict])
    data2dict = LPC([xdict,ydict])
    data2dict.set_yerr(y2)
    print data2dict.data
    if type(data2dict.data) is Line2D:
        print "Go"
    #print type(data2dict.data)
    #if isinstance(data2dict, LinePlotClass):
    #    print "LinePlotClass instance"    
    #print type(data2dict)
    #print data2dict.data
    #print data2dict.ydict
    #print data2dict.xy
    

##class LinePlotClass(object):
##    '''This class is used to create a 1D line plot using matplotlib
##    The dataDict used to initialize the object is actually a list of TWO
##    dictionaries.  One is for the x-dimension and is the dummyDict if not 
##    specified, and the second is for the y-dimension.  The y-dimension may
##    contain more than one trace which constitutes the main difference between
##    this class and the matplotlib.lines.Line2D class.  I'd like to reference
##    the Line2D class explicitly but I'm unsure how to do this because of 2 
##    reasons.
##    1) What command to use i.e. plot(Line2D)?  This of course does not work.  
##    I think it is ax.add_line() but then I need to create all the other 
##    plot paramteres.  I will give it a try sometime.
##    
##    2) How to have multiple plots in this subclass and pass them to a single
##    figure.  
##    
##    Remember the main goal here is the ability to save figures and their 
##    instances and be able to recreate them in a form that may be modified.
##    '''
